import java.util.ArrayList;
import java.util.List;

class Solution {
    List<List<Integer>> ret;
    List<Integer> path;
    int t;
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        ret = new ArrayList<>();
        path = new ArrayList<>();
        t = target;
        dfs(candidates, 0, 0);
        return ret;
    }
    private void dfs(int[] candidates, int pos, int sum) {
        if (sum == t) {
            ret.add(new ArrayList<>(path));
            return;
        }
        if (sum > t || pos == candidates.length) return;

        for (int i = pos; i < candidates.length; i++) {
            path.add(candidates[i]);
            dfs(candidates, i, sum + candidates[i]);
            path.remove(path.size() - 1);
        }
    }
}